//package com.coffee.netty.util;
//
//import com.zebra.remoting.protocol.RemotingCommand;
//import io.netty.channel.Channel;
//import io.netty.channel.ChannelFuture;
//import io.netty.channel.ChannelFutureListener;
//import io.netty.util.internal.logging.InternalLogger;
//import io.netty.util.internal.logging.InternalLoggerFactory;
//
//import java.util.concurrent.ConcurrentHashMap;
//import java.util.concurrent.TimeUnit;
//import java.util.concurrent.atomic.AtomicInteger;
//
//public class NotifyProxy {
//    private static final InternalLogger logger = InternalLoggerFactory.getInstance(NotifyProxy.class);
//
//    public static NotifyProxy notifyProxy = new NotifyProxy();
//
//    private AtomicInteger atomicInteger = new AtomicInteger(0);
//    private ConcurrentHashMap<Integer, SyncZebraFuture> hashMap = new ConcurrentHashMap<>(4);
//
//
//    public RemotingCommand syncMsg(Channel cxt, RemotingCommand command) {
//        logger.info("Send Sync message: {} {}", command, cxt.toString());
//        SyncZebraFuture syncFuture = new SyncZebraFuture<>();
//        int key = atomicInteger.incrementAndGet();
//        command.addRequestId(key);
//        hashMap.put(key, syncFuture);
//        pushMessage(cxt, command);
//
//        RemotingCommand result = null; // waite
//        try {
//            result = syncFuture.get(8, TimeUnit.SECONDS);
//        } catch (InterruptedException e) {
//            logger.error("Send syncMsg error ", e);
//        }
//        return result;
//    }
//
//    public void ackSyncMsg(Channel channel, RemotingCommand command) {
//        logger.info("ACK Sync message: {} {}", command, channel.toString());
//        SyncZebraFuture syncFuture = hashMap.get(command.requestId());   // 从缓存中获取数据
//        if(syncFuture != null) {
//            syncFuture.setResponse(command);
//            hashMap.remove(command.requestId());   //主动释放
//        }else {
//            logger.error("SyncZebraFuture is null {}", channel.toString());
//        }
//    }
//
//    public void pushMessage(Channel cxt, RemotingCommand message) {
//        ChannelFuture cf = cxt.writeAndFlush(message);
//        cf.addListener(new ChannelFutureListener() {
//            @Override
//            public void operationComplete(ChannelFuture future) throws Exception {
//                if (future.isSuccess()) {
//                    if (logger.isDebugEnabled()){
//                        logger.debug("push msg success: {} {}", message.toString(), RemotingUtil.channelToString(cxt));
//                    }
//                } else {
//                    logger.error("push Message failure: {} {}", message.toString(), RemotingUtil.channelToString(cxt));
//                }
//            }
//        });
//    }
//
//
//
//}
